package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.bean.CartBean;
import com.qf.pojo.User;
import com.qf.service.CartService;
import com.qf.service.Impl.CartServiceImpl;
import com.qf.service.Impl.OrdersServiceImpl;
import com.qf.service.OrdersService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/ordersAddController.action")
public class OrdersAddController extends HttpServlet {

    private CartService cartService = new CartServiceImpl();
    private OrdersService ordersService = new OrdersServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //如果使用的是aid
        String aid = req.getParameter("aid");
        //aid = "1";

        //从session中获取用户对象
        HttpSession session = req.getSession(false);
        User user = (User)session.getAttribute("user");
        //获取购物车中的所有数据
        List<CartBean> cartBeanList = cartService.findAll(user.getUid());

        //调用service层方法
        ordersService.add(Integer.parseInt(aid),user.getUid(),cartBeanList);

        //创建Map集合
        HashMap<String, Object> map = new HashMap<>();
        map.put("state",200);

        resp.getWriter().write(JSON.toJSONString(map));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
